package Planner;

import Query.*;
import Parse.*;
import Absyn.Print;
import ErrorMsg.ErrorMsg;

public class Planner {
	private QueryPlanner qplanner;
	private UpdatePlanner uplanner;
	
	public Planner(QueryPlanner qplanner, UpdatePlanner uplanner) {
		this.qplanner = qplanner;
		this.uplanner = uplanner;
	}
	
	/**
	 * Creates a plan for an SQL select statement, using the supplied planner.
	 * @param qry the SQL query string
	 * @param tx the transaction
	 * @return the scan corresponding to the query plan
	 */
	public Plan createQueryPlan(ErrorMsg errormsg) {
		Grm parser = new Grm(errormsg);
		java_cup.runtime.Symbol symbol;
		symbol=parser.debug_parse();
		QueryData data = parser.query();
		return qplanner.createPlan(data);
	}
	
	/**
	 * Executes an SQL insert, delete, modify, or
	 * create statement.
	 * The method dispatches to the appropriate method of the
	 * supplied update planner,
	 * depending on what the parser returns.
	 * @param cmd the SQL update string
	 * @param tx the transaction
	 * @return an integer denoting the number of affected records
	 */
	public int executeUpdate(ErrorMsg errormsg) {
		Grm parser = new Grm(errormsg);
		java_cup.runtime.Symbol symbol;
		Absyn.Data data;
		symbol=parser.debug_parse();
		Object obj = data.updateCmd(symbol);
		if (obj instanceof InsertData)
			return uplanner.executeInsert((InsertData)obj);
		else if (obj instanceof DeleteData)
			return uplanner.executeDelete((DeleteData)obj);
		else if (obj instanceof ModifyData)
			return uplanner.executeModify((ModifyData)obj);
		else if (obj instanceof CreateTableData)
			return uplanner.executeCreateTable((CreateTableData)obj);
		else
			return 0;
	}
}
